//#include "lspmessage.pb-c.h"
//*********************************************
//ADRIAN SALAZAR && JUSTIN MICHALE SCHWARTZBECK
//HOMEWORK 2
//FEB. 23, 2012
//
//Packet Class:	Provides a method to communicate
//	through the use of a data packet
//*********************************************

#include "packet.h"
#include <string.h>
#include <string>
#include <stdio.h>
using namespace std;

//Class Constructor
Packet::Packet(const uint8_t * buf, size_t bytes) {
	//memcpy(&msg, buf, bytes);
	string s;
	for(int i = 0; i < bytes; i++)
		s.push_back(buf[i]);
	//data = new uint8_t[bytes+1];
	msg.ParseFromString(s);
	//*((uint32_t*)data) = msg.connid();
	//*((uint32_t*)(data+4)) = msg.seqnum();
	//printf("Getting data from result\n");
	//memcpy(data+8, msg.payload().c_str(), bytes-sizeof(LSPMessage));
	len = bytes;
}

//Class Constructor
Packet::Packet(uint32_t id, uint32_t seq, const uint8_t* payload, size_t length) {
	len = 8+length;
	data = new uint8_t[8+length+1];
	memset(data, 0, 8+length+1);
	memcpy(data, &id, 4);
	memcpy(data+4, &seq, 4);
	memcpy(data+8, payload, length);
	msg.set_seqnum(seq);
	msg.set_connid(id);
	msg.set_payload(payload, length);
}

//Get Conn ID
uint32_t Packet::get_conn_id() {
	//return *((uint32_t*)data);
	return msg.connid();
}

//Get the Seq. Num
uint32_t Packet::get_seq_num() {
	//return *((uint32_t*)(data+4));
	return msg.seqnum();
}

//Get the Payload
const uint8_t * Packet::get_payload() {
	//return (data+8);
	return (uint8_t*) msg.payload().c_str();
}
